import React, { Component } from 'react';

export default class App extends Component {
  constructor(props){
    super(props);
    // 创建一个引用
    this.inputRef = React.createRef();
  };
  handleFocus = () => {
    this.inputRef.current.focus();
  }
  render() {
    return (
      <div>
        {/* 绑定引用到元素上 */}
        <input type="text" ref = {this.inputRef}/>
        <button onClick = {this.handleFocus}>自动聚焦</button>
      </div>
    );
  }
}


